package com.dhcc.core.framework.lucene;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.core.framework.lucene.util.IndexObject;

/**
 * LuceneService
 * 
 * @ClassName: LuceneService
 * @Description: TODO
 * @author: cyf
 * @date: 2018年3月22日 下午7:53:04
 */
@Service
public class LuceneService {

    @Autowired
    @Qualifier("luceneSearcher")
    private ISearcher luceneSearcher;

    /**
     * 创建索引
     * 
     * @Title: create
     * @Description: TODO
     * @param indexObj
     * @return: void
     */
    public void create(IndexObject indexObj) {
        checkSearcher();
        luceneSearcher.create(indexObj);
    }

    /**
     * 根据ID删除
     * 
     * @Title: delete
     * @Description: TODO
     * @param indexObjId
     * @return: void
     */
    public void delete(String indexObjId) {
        checkSearcher();
        luceneSearcher.delete(indexObjId);
    }

    /**
     * 更新检索文档
     * 
     * @Title: update
     * @Description: TODO
     * @param indexObj
     * @return: void
     */
    public void update(IndexObject indexObj) {
        checkSearcher();
        luceneSearcher.update(indexObj);
    }

    /**
     * 分页搜索
     * 
     * @param pageNum
     *            当前页
     * @param pageSize
     *            每页条数
     * @param keyword
     *            关键字
     * @return
     */
    public Page<IndexObject> search(int pageNum, int pageSize, String keyword,String siteId) {
        checkSearcher();
        return luceneSearcher.search(pageNum, pageSize, keyword, siteId);
    }

    /**
     * 初始化检查
     */
    public void checkSearcher() {
        if (luceneSearcher == null) {
            throw new RuntimeException("must init searcher before,please invoke SearchFactory.use() to init.");
        }
    }

    /**
     * 重检索引
     */
    public void reloadIndex(List<IndexObject> list) {
        checkSearcher();
        luceneSearcher.reloadIndex(list);
    }

}
